//
//  MoviePlayViewController.m
//  HdMovie
//
//  Created by Classic on 1/14/13.
//  Copyright (c) 2013 Hoang tuan. All rights reserved.
//

#import "MoviePlayViewController.h"
@interface MoviePlayViewController ()

@end

MPMoviePlayerController *moviePlayer;
@implementation MoviePlayViewController
@synthesize movie;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    //an thanh trang thai tren cung
    [[UIApplication sharedApplication] setStatusBarHidden:YES];
    NSLog(@"%@",movie.title);
    //link movie
    NSString *movieUrl = @"https://s3.amazonaws.com/adplayer/colgate.mp4";
    NSURL *URL = [NSURL URLWithString:movieUrl];
    moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:URL];
    //dang ky notification cho movieplay
    //khi movieplay da chay xong
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
    //khi movieplay thay doi trang thai
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayChangeStatus:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:moviePlayer];
    //lay bo dieu khien mac dinh
    moviePlayer.controlStyle=MPMovieControlStyleDefault;
    [moviePlayer prepareToPlay];
    //kick thuoc hien thi cua movieplay
    [moviePlayer.view setFrame:CGRectMake(0, 0, self.view.bounds.size.height, self.view.bounds.size.width)];
    //add vao view
    [self.view addSubview:moviePlayer.view];

    [moviePlayer play];
    //xoa nut back khoi view
    [_btnback removeFromSuperview];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated; {

    //xoay man hinh 90 do
    CGAffineTransform toLandscape = CGAffineTransformMakeRotation(1.57);

    [self.view setTransform:toLandscape];
    [self.view setFrame:CGRectMake(-300, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
}
- (IBAction)backClick:(id)sender {
    //hien thanh trang thai
    [[UIApplication sharedApplication] setStatusBarHidden:NO];
    //xoa view
    [self dismissModalViewControllerAnimated:YES];
}
- (void)dealloc {
    moviePlayer = nil;
    self.btnback = nil;
    [super dealloc];
}
- (void)viewDidUnload {
    [self setBtnback:nil];
    [super viewDidUnload];
}
// khi movie thay doi trang thai
-(void) moviePlayChangeStatus:(NSNotification *)notification
{
    MPMoviePlayerController *player = [notification object];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification object:player];
    if (moviePlayer.playbackState == MPMoviePlaybackStatePlaying) {
        [_btnback removeFromSuperview];
    }else{
        [self.view addSubview:_btnback];
    }
}
//khi movie da choi xong
- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
    
    MPMoviePlayerController *player = [notification object];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification object:player];
    
    if ([player respondsToSelector:@selector(setFullscreen:animated:)])
    {
        [self.view addSubview:_btnback];
    }
}
@end
